package com.scm.biz.service._impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.scm.biz.bean.BusinessFile;
import com.scm.biz.bean.DesignOrder;
import com.scm.biz.bean.Order;
import com.scm.biz.bean.OrderTaskPerformer;
import com.scm.biz.bean.ProductionBeforePatternSize;
import com.scm.biz.bean.ProductionOrder;
import com.scm.biz.bean.ProductionPattern;
import com.scm.biz.bean.ProductionPatternSize;
import com.scm.biz.bean.SysUser;
import com.scm.biz.dao.BusinessFileDao;
import com.scm.biz.dao.CustomerDao;
import com.scm.biz.dao.OrderDao;
import com.scm.biz.dao.OrderPatternDao;
import com.scm.biz.dao.OrderTaskPerformerDao;
import com.scm.biz.dao.ProductionBeforePatternSizeDao;
import com.scm.biz.dao.ProductionOrderDao;
import com.scm.biz.dao.ProductionPatternDao;
import com.scm.biz.dao.ProductionPatternSizeDao;
import com.scm.biz.dto.MsgNoticeDto;
import com.scm.biz.dto.ProductionNoticeDto;
import com.scm.biz.enums.FileCategoryEnum;
import com.scm.biz.enums.OrderStatusEnum;
import com.scm.biz.enums.OrderTypeEnum;
import com.scm.biz.result.ProductionDetailResult;
import com.scm.biz.result.ProductionNoticeResult;
import com.scm.biz.service.BusinessFileService;
import com.scm.biz.service.MsgNoticeService;
import com.scm.biz.service.OrderTaskFlowService;
import com.scm.biz.service.OrderTaskPerformerService;
import com.scm.biz.service.ProductionPatternService;
import com.scm.core.bean.exp.UfdmException;
import com.scm.utils.UfdmDateUtil;
import com.scm.utils.UploadAndDownUtil;

@Service
public class ProductionPatternServiceImpl implements ProductionPatternService {
	@Autowired
	ProductionPatternDao productionPatternDao;
	@Autowired
	ProductionOrderDao productionOrderDao;
	@Autowired
	OrderPatternDao orderPatternDao;
	@Autowired
	ProductionBeforePatternSizeDao productionBeforePatternSizeDao;
	@Autowired
	ProductionPatternSizeDao productionPatternSizeDao;

	@Resource
	OrderTaskFlowService orderTaskFlowService;

	@Autowired
	BusinessFileDao businessFileDao;
	@Autowired
	BusinessFileService businessFileService;

	@Autowired
	OrderDao orderDao;
	@Autowired
	MsgNoticeService msgNoticeService;
	@Autowired
	CustomerDao customerDao;
	@Autowired
	OrderTaskPerformerService orderTaskPerformerService;
	@Override
	public void insert(ProductionPattern productionPattern) {
		productionPatternDao.insert(productionPattern);
	}

	@Override
	public void update(ProductionPattern productionPattern) {
		productionPatternDao.updateByPrimaryKeySelective(productionPattern);
	}

	@Override
	public ProductionPattern selectById(Long id) {
		return productionPatternDao.selectByPrimaryKey(id);
	}

	@Override
	public List<ProductionPattern> selectByProductionOrderId(Long productionOrderId) {
		// TODO Auto-generated method stub
		return productionPatternDao.selectByProductionOrderId(productionOrderId);
	}

	@Override
	public List<ProductionDetailResult> selectProductionDetail(List<Long> ids) {
		List<ProductionDetailResult> productionDetailResults = productionPatternDao.selectProductionDetail(ids);
		return productionDetailResults;
	}

	@Override
	public void insertProductionNotice(List<ProductionNoticeDto> productionNoticeDtos) throws UfdmException {
		for (ProductionNoticeDto noticeDto : productionNoticeDtos) {
			// 修改对应的大货款式信息
			productionPatternDao.updateByPrimaryKeySelective(noticeDto.getProductionPattern());
			// 大货款式Id
			Integer productionId = noticeDto.getId();
			ProductionPattern productionPattern = productionPatternDao.selectByPrimaryKey((long) productionId);
			// 修改订单款式表信息
			noticeDto.getOrderPattern().setId(productionPattern.getOrderPatternId());
			//
			if (productionPattern.getStatus() == OrderStatusEnum.PRODUCTION_CREATED.value()) {
				
				SysUser sysUser = (SysUser)SecurityUtils.getSubject().getSession().getAttribute("sysUser");
				
				
				OrderTaskPerformer orderTaskPerformer = new OrderTaskPerformer();
				orderTaskPerformer.setAction("F300");
				orderTaskPerformer.setBusinessId(productionPattern.getId());
				orderTaskPerformer.setBusinessType(OrderTypeEnum.T3.toString());
				orderTaskPerformer.setCreatedAt(new Date());
				orderTaskPerformer.setPerformer(sysUser.getId());
				orderTaskPerformer.setUpdatedAt(new Date());
				orderTaskPerformer.setOrderId(productionOrderDao.selectByPrimaryKey(productionPattern.getProductionOrderId()).getOrderId());
				orderTaskPerformerService.addPerformer(orderTaskPerformer);
				
				productionPattern.setStatus(OrderStatusEnum.PRODUCTION_TM.value());
				orderTaskFlowService.addPerformer(OrderTypeEnum.T3, "F301","R005", productionPattern.getId());
				productionPatternDao.updateByPrimaryKey(productionPattern);
			}
			orderPatternDao.updateByPrimaryKeySelective(noticeDto.getOrderPattern());
			// 删除产前样尺码
			productionBeforePatternSizeDao.deleteByProductionPatternId(productionId);
			for (ProductionBeforePatternSize beforePatternSize : noticeDto.getProductionBeforePatternSizes()) {
				// 插入产前尺码
				productionBeforePatternSizeDao.insertSelective(beforePatternSize);
			}
			// 删除大货尺码
			productionPatternSizeDao.deleteByProductionPatternId(productionId);
			for (ProductionPatternSize patternSize : noticeDto.getProductionPatternSizes()) {
				// 插入大货尺码
				productionPatternSizeDao.insertSelective(patternSize);
			}
			if(noticeDto.getFile()!=null&&!noticeDto.getFile().isEmpty()){
				// 保存工厂合同
				businessFileDao.deleteByBusinessId((long)noticeDto.getId(), FileCategoryEnum.PRODUCTION_CONTRACT_FILE.value());
				businessFileService.insert(noticeDto.getFile(), Long.valueOf(noticeDto.getId()), FileCategoryEnum.PRODUCTION_CONTRACT_FILE.value(), 0);
			}
		}
	}

	@Override
	public List<ProductionNoticeResult> selectProdutionNotice(List<Long> ids) {
		return productionPatternDao.selectProdutionNotice(ids);
	}

	@Override
	public void saveTmFile(ProductionPattern productionPattern, MultipartFile file) throws UfdmException {
		String path = UploadAndDownUtil.uploadFile(file, FileCategoryEnum.PRODUCTION_TM_FILE.value());
		productionPattern.setPushCodeFile(path);
		if (productionPattern.getStatus() <= OrderStatusEnum.PRODUCTION_SC.value()) {
			ProductionOrder productionOrder = productionOrderDao.selectByPrimaryKey(productionPattern.getProductionOrderId());
			Order order = orderDao.selectByPrimaryKey(productionOrder.getOrderId());
			productionPattern.setStatus(OrderStatusEnum.PRODUCTION_SC.value());
			orderTaskFlowService.addPerformer(OrderTypeEnum.T3, "F302",order.getManager(), productionPattern.getId());
			orderTaskFlowService.addPerformer(OrderTypeEnum.T3, "F306","R004", productionPattern.getId());
			

			/********************消息***************/
			SysUser sysUser = (SysUser)SecurityUtils.getSubject().getSession().getAttribute("sysUser");
			
			OrderTaskPerformer orderTaskPerformer = new OrderTaskPerformer();
			orderTaskPerformer.setAction("F301");
			orderTaskPerformer.setBusinessId(productionPattern.getId());
			orderTaskPerformer.setBusinessType(OrderTypeEnum.T3.toString());
			orderTaskPerformer.setCreatedAt(new Date());
			orderTaskPerformer.setPerformer(sysUser.getId());
			orderTaskPerformer.setUpdatedAt(new Date());
			orderTaskPerformer.setOrderId(order.getId());
			orderTaskPerformerService.addPerformer(orderTaskPerformer);
			
			
			
			
			
			MsgNoticeDto msgNoticeDto = new MsgNoticeDto();
			msgNoticeDto.setTitle("推码表已上传");
			msgNoticeDto.setContent(sysUser.getName()+"已经上传"+customerDao.selectByPrimaryKey(order.getCustomerId().intValue()).getFullname()
									+ "客户的大货订单推码表，请跟进项目进度");
			msgNoticeDto.setOrderType(OrderTypeEnum.T3.toString());
			msgNoticeDto.setTaskId(productionOrder.getId());
			msgNoticeDto.setUserType(0);
			msgNoticeDto.setOrderId(order.getId());
			msgNoticeService.insert(msgNoticeDto,order.getManager());
		}
		productionPatternDao.updateByPrimaryKeySelective(productionPattern);
		
		
	}

	@Override
	public void saveLastProduce(ProductionPattern productionPattern, MultipartFile[] lastFile,MultipartFile[] middleFile) throws UfdmException {
		// 如果已提交到下一状态，就不能在提交
		if (lastFile !=null && lastFile.length >0 && productionPattern.getStatus() <= OrderStatusEnum.PRODUCTION_FH.value() && businessFileDao
				.selectByBusinessId(productionPattern.getId(), FileCategoryEnum.PROOFING_LAST_FILE.value())
				.size() == 0) {
			

			ProductionOrder productionOrder = productionOrderDao.selectByPrimaryKey(productionPattern.getProductionOrderId());
			Order order = orderDao.selectByPrimaryKey(productionOrder.getOrderId());
			
			productionPattern.setStatus(OrderStatusEnum.PRODUCTION_FH.value());
			orderTaskFlowService.addPerformer(OrderTypeEnum.T3, "F303",order.getManager(), productionPattern.getId());
			
			/********************消息***************/
			SysUser sysUser = (SysUser)SecurityUtils.getSubject().getSession().getAttribute("sysUser");

			OrderTaskPerformer orderTaskPerformer = new OrderTaskPerformer();
			orderTaskPerformer.setAction("F306");
			orderTaskPerformer.setBusinessId(productionPattern.getId());
			orderTaskPerformer.setBusinessType(OrderTypeEnum.T3.toString());
			orderTaskPerformer.setCreatedAt(new Date());
			orderTaskPerformer.setPerformer(sysUser.getId());
			orderTaskPerformer.setUpdatedAt(new Date());
			orderTaskPerformer.setOrderId(order.getId());
			orderTaskPerformerService.addPerformer(orderTaskPerformer);
			
			MsgNoticeDto msgNoticeDto = new MsgNoticeDto();
			msgNoticeDto.setTitle("大货质检通过");
			msgNoticeDto.setContent(customerDao.selectByPrimaryKey(order.getCustomerId().intValue()).getFullname()
									+ "客户的大货订单质检已通过，请联系工厂填写装箱单");
			msgNoticeDto.setOrderType(OrderTypeEnum.T3.toString());
			msgNoticeDto.setTaskId(productionOrder.getId());
			msgNoticeDto.setUserType(0);
			msgNoticeDto.setOrderId(order.getId());
			msgNoticeService.insert(msgNoticeDto,order.getManager());
			
		}
		if(lastFile !=null){
			businessFileService.insertList(Arrays.asList(lastFile), productionPattern.getId(),
					FileCategoryEnum.PROOFING_LAST_FILE.value(), 0);
		}
		
		if(middleFile !=null){
			businessFileService.insertList(Arrays.asList(middleFile), productionPattern.getId(),
					FileCategoryEnum.PROOFING_MIDDLE_FILE.value(), 0);
		}
		
		productionPatternDao.updateByPrimaryKey(productionPattern);
	
	}

	@Override
	public void saveProduceDate(ProductionPattern productionPattern){
		ProductionPattern pp = productionPatternDao.selectByPrimaryKey(productionPattern.getId());
		productionPatternDao.updateByPrimaryKey(productionPattern);
		
		/********************消息***************/
		SysUser sysUser = (SysUser)SecurityUtils.getSubject().getSession().getAttribute("sysUser");
		ProductionOrder productionOrder = productionOrderDao.selectByPrimaryKey(productionPattern.getProductionOrderId());
		Order order = orderDao.selectByPrimaryKey(productionOrder.getOrderId());
		
		OrderTaskPerformer orderTaskPerformer = new OrderTaskPerformer();
		orderTaskPerformer.setAction("F302");
		orderTaskPerformer.setBusinessId(productionPattern.getId());
		orderTaskPerformer.setBusinessType(OrderTypeEnum.T3.toString());
		orderTaskPerformer.setCreatedAt(new Date());
		orderTaskPerformer.setPerformer(sysUser.getId());
		orderTaskPerformer.setUpdatedAt(new Date());
		orderTaskPerformer.setOrderId(order.getId());
		orderTaskPerformerService.addPerformer(orderTaskPerformer);
		
		
		MsgNoticeDto msgNoticeDto = new MsgNoticeDto();
		
		msgNoticeDto.setOrderType(OrderTypeEnum.T3.toString());
		msgNoticeDto.setTaskId(productionOrder.getId());
		msgNoticeDto.setUserType(1);
		if(pp.getProduceStartdate()==null && productionPattern.getProduceStartdate() !=null){
			msgNoticeDto.setTitle("大货已开始生产");
			msgNoticeDto.setContent(customerDao.selectByPrimaryKey(order.getCustomerId().intValue()).getFullname()
									+ "客户的大货订单已经开始生产，时间为"+UfdmDateUtil.dateToString(productionPattern.getProduceStartdate(), "MM月dd日"));
		}
		if(pp.getFirstSample()==null && productionPattern.getFirstSample() !=null){
			msgNoticeDto.setTitle("大货已完成首件样");
			msgNoticeDto.setContent(customerDao.selectByPrimaryKey(order.getCustomerId().intValue()).getFullname()
									+ "客户的大货订单已经完成首件样，时间为"+UfdmDateUtil.dateToString(productionPattern.getProduceStartdate(), "MM月dd日"));
		}
		if(pp.getProduceEnddate()==null && productionPattern.getProduceEnddate() !=null){
			msgNoticeDto.setTitle("大货已完成生产");
			msgNoticeDto.setContent(customerDao.selectByPrimaryKey(order.getCustomerId().intValue()).getFullname()
									+ "客户的大货订单已经完成生产，时间为"+UfdmDateUtil.dateToString(productionPattern.getProduceEnddate(), "MM月dd日"));
		}
		msgNoticeDto.setOrderId(order.getId());
		msgNoticeService.insert(msgNoticeDto,"R004");
	}

}
